package org.coonect;

import java.awt.*;

import java.sql.*;
import java.util.*;
import javax.swing.*;

//import javax.swing.table.*;

@SuppressWarnings("serial")
public class Getdata extends JFrame {
	
	
	
	@SuppressWarnings("unchecked")
	public Getdata() {
		Vector columnNames = new Vector();
		Vector data = new Vector();
		

		try {
			// Connect to an Access Database

			// String driver = "sun.jdbc.mysql.JdbcMysqlDriver";
			// String url = "jdbc:odbc:???"; // if using ODBC Data Source name
			String url = "jdbc:h2:~/test?useEncoding=true&characterEncoding=UTF-8";
			String userid = "sa";
			String password = "";

			// Class.forName( driver );
			Connection connection = DriverManager.getConnection(url, userid,
					password);

			// Read data from a table

			String sql = "Select NASLOV,TEKST,DATUM_POCETKA, DATUM_KRAJA from DATABASE1";
			Statement stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			ResultSetMetaData md = rs.getMetaData();
			int columns = md.getColumnCount();

			// Get column names

			for (int i = 1; i <= columns; i++) {
				columnNames.addElement(md.getColumnName(i));
			}

			// Get row data

			while (rs.next()) {
				Vector row = new Vector(columns);

				for (int i = 1; i <= columns; i++) {
					row.addElement(rs.getObject(i));

				}

				data.addElement(row);
			}

			rs.close();
			stmt.close();
			connection.close();
		} catch (Exception e) {
			System.out.println(e);
		}

		// Create table with database data

		JTable table = new JTable(data, columnNames) {

			public Class getColumnClass(int column) {
				for (int row = 0; row < getRowCount(); row++) {
					Object o = getValueAt(row, column);

					if (o != null) {
						return o.getClass();

					}
				}

				return Object.class;
			}
		};

		JScrollPane scrollPane = new JScrollPane(table);
		getContentPane().add(scrollPane);

		JPanel buttonPanel = new JPanel();
		getContentPane().add(buttonPanel, BorderLayout.SOUTH);
	}

	public static void main(String[] args) {
		
		
		Getdata frame = new Getdata();
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

		frame.pack();
		frame.setVisible(true);
		
		
	}
}


